# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _
#----------------------------------------------------------
# Products
#----------------------------------------------------------

class product_template(osv.osv):
    _inherit = "product.template"

    _columns = {
        'type': fields.selection([('product','Stockable Product'),('service','Service')], 'Product Type', required=True, help="a service is a non-material product provided by a company or an individual. \nStockable product: Will imply stock management for this product."),
        'procure_method': fields.selection([('make_to_stock','Make to Stock'),('make_to_order','Make to Order')], 'Procurement Method', required=True, invisible=True, help="Make to Stock: When needed, the product is taken from the stock or we wait for replenishment. \nMake to Order: When needed, the product is purchased or produced."),
        'supply_method': fields.selection([('produce','Manufacture'),('buy','Buy')], 'Supply Method', required=True, invisible=True, help="Manufacture: When procuring the product, a manufacturing order or a task will be generated, depending on the product type. \nBuy: When procuring the product, a purchase order will be generated."),
        'cost_method': fields.selection([('standard','Standard Price'), ('average','Average Price'), ('fifo','FIFO(First In First Out)'), ('lifo','LIFO(Last In First Out)')], 'Costing Method', required=True,
            help="Standard Price: The cost price is manually updated at the end of a specific period (usually every year). \nAverage Price: The cost price is recomputed at each incoming shipment."),
    }

    _defaults = {
        'type' : 'product',
    }

product_template()

class product_product(osv.osv):
    _inherit = "product.product"

    _columns = {
        'brand_name': fields.related('brand_id','name', type='char', readonly=True, size=64, relation='product.brand', string='Brand Name'),
        'brand_id': fields.many2one('product.brand','Brand', required=True, change_default=True,help="Select brand for the current product"),
    }

#Duplicated Name
    def _check_name(self, cursor, user, ids, context=None):
        for product in self.browse(cursor, user, ids, context=context):
            if self.search(cursor, user, [('name', '=', product.name),('id', '!=', product.id)]):
                return False
        return True

    _constraints = [
        (_check_name, 'Error: Product Name must be unique per Product Name!', ['name']),
    ]

product_product()

#----------------------------------------------------------
# Product Brand
#----------------------------------------------------------
class product_brand(osv.osv):
    _name = "product.brand"
    _description = "Product Brand"

    _columns = {
        'name': fields.char('Name', size=64, required=True, translate=True, select=True),
        'description': fields.text('Description',translate=True),
    }

#Delete
    def _check_moves(self, cr, uid, ids, method, context=None):
        product_obj = self.pool.get('product.product')
        if product_obj.search(cr, uid, [('brand_id', 'in', ids)]):
            if method == 'unlink':
                raise osv.except_osv(_('Error!'), _('You cannot remove an Product Brand that related with Product.'))
        return True
    
    def unlink(self, cr, uid, ids, context=None):
        self._check_moves(cr, uid, ids, "unlink", context=context)
        return super(product_brand, self).unlink(cr, uid, ids, context=context)

#Duplicated Name
    def _check_name(self, cursor, user, ids, context=None):
        for part in self.browse(cursor, user, ids, context=context):
            if self.search(cursor, user, [('name', '=', part.name),('id', '!=', part.id)]):
                return False
        return True

    _constraints = [
        (_check_name, 'Error: Product Brand must be unique per Product Brand!', ['name']),
    ]

#Copy
    def copy(self, cr, uid, id, default=None, context=None):
        if context is None:
            context={}

        if not default:
            default = {}

        # Craft our own `<name> (copy)` in en_US (self.copy_translation()
        # will do the other languages).
        context_wo_lang = context.copy()
        context_wo_lang.pop('lang', None)
        car = self.read(cr, uid, id, ['name'], context=context_wo_lang)
        default = default.copy()
        default.update(name=_("%s (copy)") % (car['name']))

        return super(product_brand, self).copy(cr, uid, id, default=default,
                context=context)

product_brand()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
